package com.atguigu.juc.chap10;

import java.util.concurrent.CompletableFuture;

public class CompletableFutureDemo08 {

	public static void main(String[] args) {

        //CompletableFuture内置线程池对象
        CompletableFuture.supplyAsync(() -> {
            System.out.println("第一个任务执行");
            return "first task result";
        }).thenApply((String preTaskResult)->{

            System.out.println("第二个任务执行");
            int a = 10 / 0;
            return preTaskResult;
        }).handle((String preTaskResult, Throwable throwable)->{

            System.out.println("handle");
            System.out.println(Thread.currentThread().getName());
            if(throwable != null){
                System.out.println(throwable.getClass().getName());
                System.out.println(throwable.getCause().getClass().getName());
                System.out.println(throwable.getMessage());
                return "兜底的结果";
            }else{
                return preTaskResult;
            }
        }).whenComplete((String preTaskResult, Throwable throwable) -> {

            System.out.println(" 获取结果 preTaskResult = " + preTaskResult);
            System.out.println(" throwable = " + throwable);
        });
    }
}